﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Xml;

namespace VoIPWeb.Uc
{
    public partial class LoginUc : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == true)
            {
                this.lbError.Visible = false;

                var v_str_customer = Request.Form["txtUsername"];
                var v_str_slit_customer = v_str_customer.Split('*');
                if (v_str_slit_customer.Length == 1)
                {
                    lbError.Text = "Please fill your username with your reseller. Ex: user*reseller";
                    lbError.Visible = true;
                    return;
                }
                var v_str_user = v_str_slit_customer[1];
                var v_str_cpasswd = Request.Form["txtPassword"];
                var v_str_cpass_user = "samsung5568";
                var v_str_api = string.Format("https://www.voipinfocenter.com/api/request.ashx?command=ValidateUser&username={0}&password={1}&customer={2}&customerpassword={3}", v_str_user, v_str_cpass_user, v_str_customer, v_str_cpasswd);

                HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(v_str_api);
                HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
                XmlDataDocument dataXML = new XmlDataDocument();

                dataXML.Load(HttpWResp.GetResponseStream());
                XmlNodeList v_xml_nodelist = dataXML.SelectNodes("//Result");

                // Đăng nhập thành công
                if (v_xml_nodelist.Count > 0 && v_xml_nodelist[0].InnerText.Equals("Success") == true)
                {
                    v_str_api = string.Format("https://www.voipinfocenter.com/api/request.ashx?command=GetUserInfo&username={0}&password={1}&customer={2}", v_str_user, v_str_cpass_user, v_str_customer);
                    HttpWReq = (HttpWebRequest)WebRequest.Create(v_str_api);
                    HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
                    dataXML = new XmlDataDocument();
                    dataXML.Load(HttpWResp.GetResponseStream());

                    v_xml_nodelist = dataXML.SelectNodes("//Blocked");

                    if (v_xml_nodelist.Count > 0 && v_xml_nodelist[0].InnerText.Equals("False") == true)
                    {
                        Session["Login"] = true;
                        Session["User"] = v_str_user;
                        Session["Customer"] = v_str_customer;
                        Session["Cpassword"] = v_str_cpasswd;
                        if (string.IsNullOrEmpty(this.NextPageIfSuccess) == false)
                            Response.Redirect(this.NextPageIfSuccess);
                    }
                    else
                    {
                        this.lbError.Visible = true;
                        this.lbError.Text = "Your account have been locked!";
                    }
                }
                else // Đăng nhập không thành công
                {
                    var v_reason = dataXML.SelectSingleNode("//Reason");
                    this.lbError.Visible = true;
                    this.lbError.Text = v_reason.InnerText;
                    // Chuyển sang trang khác nếu không thành công
                    if (string.IsNullOrEmpty(this.NextPageIfUnSuccess) == false)
                        Response.Redirect(this.NextPageIfUnSuccess);
                }
            }

        }

        public bool EnableLabelError
        {
            get { return this.lbError.Enabled; }
            set { this.lbError.Enabled = value; }
        }

        public string NextPageIfSuccess
        {
            get;
            set;
        }

        public string NextPageIfUnSuccess
        {
            get;
            set;
        }
    }
}